const { dbQuery } = require('./src/config/database');

async function clearOldImageUrls() {
  try {
    console.log('开始清空旧的image_url字段...');
    
    // 清空所有指向旧路径的image_url字段
    const updateQuery = `UPDATE menu_items SET image_url = NULL WHERE image_url LIKE '/images/menu/%' AND deleted_at IS NULL`;
    console.log(`执行SQL: ${updateQuery}`);
    
    const result = await dbQuery(updateQuery);
    console.log(`更新结果:`, result);
    console.log(`已清空 ${result.affectedRows} 个菜品的旧图片URL`);
    
    // 验证结果
    const checkQuery = `SELECT COUNT(*) as count FROM menu_items WHERE image_url LIKE '/images/menu/%' AND deleted_at IS NULL`;
    const checkResult = await dbQuery(checkQuery);
    console.log(`验证结果: 剩余旧路径数量 = ${checkResult[0].count}`);
    
    process.exit(0);
  } catch (err) {
    console.error('清空失败:', err);
    process.exit(1);
  }
}

clearOldImageUrls();